﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrmDocman.DataCore;

/// <summary>
/// Summary description for LoaiSanPhamHelper
/// </summary>
public class LoaiSanPhamHelper : TWSData
{
	public LoaiSanPhamHelper()
	{
        m_dao = DataBridge.GetInstance();
	}

    public LoaiSanPhamDataSet LoadLoaiSanPham()
    {

        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();

        m_dao.FillDataTable(lspDs.LoaiSanPham);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public LoaiSanPhamDataSet LoadLoaiSanPham(int loaisanphamID)
    {

        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
        sql += " where LoaiSanPhamID = " + loaisanphamID;

        m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public LoaiSanPhamDataSet LoadLoaiSanPhamCha()
    {
        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
        sql += " where LoaiSanPhamID in (select LoaiSanPhamChaID from dbo.LoaiSanPham where 1=1 ) ";

        m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public LoaiSanPhamDataSet LoadLoaiSanPhamGoc()
    {
        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
        sql += " where LoaiSanPhamChaID is null ";

        m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public LoaiSanPhamDataSet LoadLoaiSanPhamCon(int loaisanphamchaID)
    {

        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
        sql += " where LoaiSanPhamChaID = " + loaisanphamchaID;

        m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public LoaiSanPhamDataSet LoadLoaiSanPhamConMenu(int loaisanphamchaID)
    {

        LoaiSanPhamDataSet lspDs = GetLoaiSanPhamCha(loaisanphamchaID);
        if (lspDs.LoaiSanPham.Rows.Count > 0)
        {
            if (lspDs.LoaiSanPham[0].IsLoaiSanPhamChaIDNull())
            {
                return LoadLoaiSanPhamCon(lspDs.LoaiSanPham[0].LoaiSanPhamID);
            }
            else
            {
                return LoadLoaiSanPhamCon(lspDs.LoaiSanPham[0].LoaiSanPhamChaID);
            }
        }
        return LoadLoaiSanPhamCon(loaisanphamchaID);
    }

    public bool SaveLoaiSanPham(LoaiSanPhamDataSet lspDs)
    {
        m_dao.UpdateDataTable(lspDs.LoaiSanPham);
        m_dao.Connection.CloseConnection();
        return true;

    }

    public bool IsLoaiSanPhamCha(int loaisanphamID)
    {
        LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
        sql += " where LoaiSanPhamID in (select LoaiSanPhamChaID from dbo.LoaiSanPham where 1=1 ) and LoaiSanPhamID = "+loaisanphamID;

        m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
        m_dao.Connection.CloseConnection();
        if (lspDs.LoaiSanPham.Rows.Count>0)
        {
            return true;
        }
        return false ;
    }

    public LoaiSanPhamDataSet GetLoaiSanPhamCha(int loaisanphamID)
    {

            LoaiSanPhamDataSet lspDs = new LoaiSanPhamDataSet();
            string sql = m_dao.BuildSelectQuery(lspDs.LoaiSanPham);
            sql += " where LoaiSanPhamID = (select LoaiSanPhamChaID from dbo.LoaiSanPham where LoaiSanPhamID = " + loaisanphamID + ")";

            m_dao.FillDataTable(lspDs.LoaiSanPham, sql);
            m_dao.Connection.CloseConnection();
            return lspDs;
        }

}